package cn.snmp.mutual.uk.stack;

import java.io.*;

public class SnmpContextv3Discovery extends SnmpContextv3Basis {
    private static final String version_id = "@(#)$Id: SnmpContextv3Discovery.java,v 3.11 2009/03/05 13:12:50 birgita Exp $ Copyright Westhawk Ltd";

    public SnmpContextv3Discovery(final String host, final int port) throws IOException {
        super(host, port);
    }

    public SnmpContextv3Discovery(final String host, final int port, final String typeSocketA) throws IOException {
        super(host, port, typeSocketA);
    }

    public SnmpContextv3Discovery(final String host, final int port, final String bindAddress, final String typeSocketA) throws IOException {
        super(host, port, bindAddress, typeSocketA);
    }

    @Override
    public Pdu processIncomingPdu(final byte[] message) throws DecodingException, IOException {
        final String msg = this.checkContextSanity();
        if (msg != null) {
            throw new DecodingException(msg);
        }
        final int l = message.length;
        final byte[] copyOfMessage = new byte[l];
        System.arraycopy(message, 0, copyOfMessage, 0, l);
        final AsnDecoderv3 rpdu = new AsnDecoderv3();
        final ByteArrayInputStream in = new ByteArrayInputStream(message);
        final AsnSequence asnTopSeq = rpdu.DecodeSNMPv3(in);
        final int msgId = rpdu.getMsgId(asnTopSeq);
        final AsnPduSequence pduSeq = rpdu.processSNMPv3(this, asnTopSeq, copyOfMessage, true);
        Pdu pdu = null;
        if (pduSeq != null) {
            final byte type = pduSeq.getRespType();
            if (type == -96 && pduSeq.isSnmpv3Discovery()) {
                pdu = new GetPdu(this);
            } else if (AsnObject.debug > 3) {
                System.out.println(this.getClass().getName() + ".ProcessIncomingPdu(): PDU received with type " + pduSeq.getRespTypeString() + ". Ignoring it.");
            }
            if (pdu != null) {
                pdu.fillin(pduSeq);
                pdu.snmpv3MsgId = new Integer(msgId);
            }
        }
        return pdu;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        SnmpContextv3Discovery clContext = null;
        try {
            clContext = new SnmpContextv3Discovery(this.hostname, this.hostPort, this.bindAddr, this.typeSocket);
            clContext = (SnmpContextv3Discovery) this.cloneParameters(clContext);
        } catch (IOException exc) {
            throw new CloneNotSupportedException("IOException " + exc.getMessage());
        }
        return clContext;
    }
}
